package com.wind.trademark.user.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wind.trademark.base.vo.NodeVo;
import com.wind.trademark.common.dao.BaseDao;
import com.wind.trademark.common.vo.PageBean;
import com.wind.trademark.common.vo.PageParam;
import com.wind.trademark.user.dao.RoleDao;
import com.wind.trademark.user.model.AccPermission;
import com.wind.trademark.user.model.AccRole;
import com.wind.trademark.user.model.AccRolePermission;
import com.wind.trademark.user.service.RoleService;



@Service
public class RoleServiceImpl implements RoleService {
	
	@Autowired
	private RoleDao RoleDao; 
	@Autowired
	private BaseDao baseDao;
	
	@Override
	public AccRole saveOrUpdateRole(AccRole role) {
		role.setCreateBy(1L);
		role.setCreateTime(new Date());
		role.setLastUpdateBy(1L);
		role.setLastUpdateTime(new Date());
		baseDao.saveOrUpdate(role);
		return role;
	}
	
	@Override
	public void updateRole(AccRole role) {
		role.setLastUpdateTime(new Date());
		 baseDao.update(role);
	}
	
	@Override
	public void deleteRole(AccRole role) {
		 baseDao.delete(role);
	}
	
	@Override
	public PageBean queryRole(PageParam pageParam, Map<String, Object> params) {
		return RoleDao.queryRole(pageParam,params);
	}
	 
	@Override
	public AccRole getRole(Long id) {
		return baseDao.get(AccRole.class, id);
	}

	@Override
	public List<NodeVo> loadPermissionsByRoleId(Long roleId) {
		List<AccRolePermission>  accRolePermissionList=queryAccRolePermissionByRoleId(roleId);
		
		
		List<NodeVo> directoryVoList=new ArrayList<NodeVo>();
		
		List<AccPermission> directoryList=this.getAccPermissionTree();
		for(AccPermission directory:directoryList){
			NodeVo directoryVo=this.convert2NodeVo(directory);
			this.checkPermission(directoryVo,accRolePermissionList);
			directoryVoList.add(directoryVo);

			List<AccPermission> menuList=directory.getChildren();
			for(AccPermission menu:menuList){
				NodeVo menuVo=this.convert2NodeVo(menu);
				this.checkPermission(menuVo,accRolePermissionList);
				directoryVo.getChildren().add(menuVo);
				
				List<AccPermission> controllList=menu.getChildren();

				for(AccPermission controll:controllList){
					NodeVo controllVo=this.convert2NodeVo(controll);
					this.checkPermission(controllVo,accRolePermissionList);
					menuVo.getChildren().add(controllVo);
				}
			}
			
		}
		
		
		return directoryVoList;
	}

	private void checkPermission(NodeVo nodeVo,
			List<AccRolePermission> accRolePermissionList) {
		for(AccRolePermission accRolePermission:accRolePermissionList){
			if(accRolePermission.getPermissionId().toString().equals(nodeVo.getId())){
				nodeVo.setChecked(true);
				break;
			}
		}
		
	}

	private NodeVo convert2NodeVo(AccPermission accPermission) {
		NodeVo nodeVo=new NodeVo();

		nodeVo.setId(accPermission.getId().toString());
		nodeVo.setText(accPermission.getName());
		nodeVo.setLeaf(accPermission.getChildren().isEmpty()?true:false);
		return nodeVo;
	}

	private List<AccRolePermission> queryAccRolePermissionByRoleId(Long roleId) {
		
		List<AccRolePermission> AccRolePermissionList=this.baseDao.findByProperty(AccRolePermission.class, "roleId", roleId);
		return AccRolePermissionList;
	}
	private List<AccPermission> queryAccPermission(Map<String, Object> params) {
		return null;
	}
	private List<AccPermission> getAccPermissionTree() {
		List<AccPermission> directoryList= this.baseDao.findByProperty(AccPermission.class, "type", "D");
//		for(AccPermission directory:directoryList){
//			List<AccPermission> menuList=directory.getChildren();
//			for(AccPermission menu:menuList){
//				baseDao.getHibernateTemplate().initialize(menu.getChildren());
//
//			}
//		}
		return directoryList;
	}

	@Override
	public void saveOrUpdateRolePermission(Long roleId, List<Long> permission) {
		List<AccRolePermission> accRolePermissionList=this.queryAccRolePermissionByRoleId(roleId);
		for(AccRolePermission accRolePermission:accRolePermissionList){
			this.baseDao.delete(accRolePermission);
		}
		for(Long permissionId:permission){
			AccRolePermission accRolePermission=new AccRolePermission();
			accRolePermission.setRoleId(roleId);
			accRolePermission.setPermissionId(permissionId);
			this.baseDao.saveOrUpdate(accRolePermission);
		}
	}

	@Override
	public List<AccRole> getAllRoles() {
		return this.baseDao.findAll(AccRole.class);
	}
	
} 